package com.beiing.tianshuai.tianshuai.message.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.GroupDelMembersAdapter;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.ContactsBean;
import com.beiing.tianshuai.tianshuai.entity.GroupDetailsBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.message.presenter.GroupDelMembersPresenter;
import com.beiing.tianshuai.tianshuai.message.view.GroupDelMembersViewImpl;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.util.PinYinUtils;
import com.beiing.tianshuai.tianshuai.widget.CustomDialog;
import com.beiing.tianshuai.tianshuai.widget.CustomProgressDialog;
import com.beiing.tianshuai.tianshuai.widget.SideLitterBarView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

public class GroupDelMembersActivity extends BaseActivity implements GroupDelMembersViewImpl {

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView mToolbarTvTitle;
    @BindView(R.id.toolbar_btn_publish)
    TextView mToolbarBtnPublish;
    @BindView(R.id.my_friends_list)
    ListView mMembersList;
    @BindView(R.id.side_navigation)
    SideLitterBarView mSideNavigation;
    @BindView(R.id.tv_center)
    TextView mTvCenter;

    private static final String TAG = "GroupDelMembersActivity";
    private List<String> mMembersChoice = new ArrayList<>();

    private Context mContext;
    private Unbinder mUnbinder;
    private String mGId;
    private String mEaseGId;
    private List<GroupDetailsBean.DataBean.NumberBean.UserBean> mMembers;
    private GroupDelMembersAdapter mAdapter;
    private GroupDelMembersPresenter mPresenter;
    private int lastPosition = -1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_group_del_members;
    }

    @Override
    protected void init() {
        mUnbinder = ButterKnife.bind(this);
        mContext = this;
        initData();
        initToolbar();
        initAdapter();
        initListener();
        initPresenter();
    }

    private void initData() {
        Intent intent = getIntent();
        mMembers = (List<GroupDetailsBean.DataBean.NumberBean.UserBean>)
                intent.getSerializableExtra("members");
        mGId = intent.getStringExtra("gId");
        mEaseGId = intent.getStringExtra("easeGId");
    }

    private void initToolbar() {
        mToolbarBtnPublish.setVisibility(View.VISIBLE);
        mToolbarIvBack.setVisibility(View.VISIBLE);
        mToolbarTvTitle.setText("删除成员");
        mToolbarBtnPublish.setText("完成");
    }

    private void initAdapter() {
        List<ContactsBean> contactsBeanList = new ArrayList<>();
        for (GroupDetailsBean.DataBean.NumberBean.UserBean member : mMembers) {
            if (UserInfoBean.getUid(mContext).equals(member.getUid())) {
                continue;
            }
            String uid = member.getUid();
            String photo = member.getPhoto();
            String unit = member.getUnit();
            String name = member.getName();
            ContactsBean contactsBean = new ContactsBean();
            contactsBean.setUid(uid);
            contactsBean.setAvatar(photo);
            contactsBean.setUnit(unit);
            contactsBean.setName(name);
            contactsBean.setFirstLetter(PinYinUtils.getPinyin(name).substring(0, 1));
            contactsBeanList.add(contactsBean);
        }
        mAdapter = new GroupDelMembersAdapter(mContext, contactsBeanList);
        mMembersList.setAdapter(mAdapter);
    }

    private void initListener() {
        //返回
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //控制器点击事件
        mAdapter.setListener(new GroupDelMembersAdapter.OnItemClick() {
            @Override
            public void onItemClickListener(int position, boolean isChecked, ContactsBean bean) {
                if (isChecked) {
                    if (lastPosition != -1) {
                        mAdapter.unCheckBox(lastPosition);
                    }
                    mMembersChoice.clear();
                    mMembersChoice.add(bean.getUid());
                    lastPosition = position;
                } else {
                    mMembersChoice.clear();
                }
            }
        });

        //提交
        mToolbarBtnPublish.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                LogUtils.i(mMembersChoice.toString(), TAG);
                CustomDialog dialog = new CustomDialog(mContext, R.style.dialog, "确定要移除这些用户吗？", new CustomDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean confirm) {
                        if (confirm) {
                            final StringBuilder members = new StringBuilder();
                            for (int i = 0; i < mMembersChoice.size(); i++) {
                                String uid = mMembersChoice.get(i);
                                if (i < mMembersChoice.size() - 1) {
                                    members.append(uid).append(",");
                                } else {
                                    members.append(uid);
                                }
                            }
                            mPresenter.getGroupDelMembersResult(
                                    UserInfoBean.getUid(mContext),
                                    members.toString(),
                                    mGId,
                                    mEaseGId
                            );
                            dialog.dismiss();
                        } else {
                            dialog.dismiss();
                        }
                    }
                }).setTitle("提示");
                dialog.setCanceledOnTouchOutside(true);
                dialog.show();
            }
        });
    }

    private void initPresenter() {
        mPresenter = new GroupDelMembersPresenter(this);
    }

    @Override
    protected void onDestroy() {
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
        super.onDestroy();
    }

    @Override
    public void showProgress() {
        CustomProgressDialog.showLoading(mContext, "正在提交，请稍后...", true);
    }

    @Override
    public void hideProgress() {
        CustomProgressDialog.stopLoading();
    }

    @Override
    public void onRequestSuccess(CodeBean bean) {
        if (bean.getCode() == 200) {
            finish();
        } else {
            showShortText("哎呀，加载请求出错了~");
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        showShortText("网络异常");
    }
}
